﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraEditors;
using EduFormManager.Models;
using EduFormManager.Models.Repo;

namespace EduFormManager.Utils
{
    public static class LevelMunitHelper
    {
        public static TileContainer CreateEduFormsTileContainer(Municipality m, WindowsUIView view, AllformType.Type.Types formType, bool isArchive = false)
        {
            string eduCaption = "Образовательные учреждения";
            string subEduCaption = "";
            TileContainer eduContainer = TileContainerHelper.CreateTileContainer(view, null, eduCaption, subEduCaption);
            IList<EduFormData> dataList = RepoHelper.Repo.GetEduFormDataByMunit(m, formType, isArchive);
            foreach (EduOrg eo in m.EduOrgs.OrderBy(x => x.ShortName))
            {
                IList<EduFormData> data = dataList.Where(x => x.EduOrg.Id == eo.Id).ToList();
                Tile eoTile = data.Count > 0 ? TileHelper.CreateAnimatedEduTile(eo, isArchive) : TileHelper.CreateEduTile(eo, isArchive);

                eoTile.Group = eo.ShortName.Substring(0, 1);
                eduContainer.Items.Add(eoTile);

                string formCaption = "Формы федерального статистического наблюдения";
                string subFormCaption = eo.ShortName;
                TileContainer formContainer = TileContainerHelper.CreateTileContainer(view, eoTile, formCaption, subFormCaption, eduContainer);
                formContainer.Tag = TagHelper.GetFormContainerTag(eo, formType, isArchive);

                GuiHelper.FormStatusCount statusCount = new GuiHelper.FormStatusCount() { FormsOkCount = 0, FormsWithErrorsCount = 0, FormsWithExpiredDateCount = 0, FormsWithBothCount = 0 };

                if (data.Count > 0)
                {
                    foreach (EduFormData fd in data)
                    {
                        Tile dTile = TileHelper.CreateFormTile(fd, view, formContainer, isArchive);
                        TileHelper.SetFormTileStatus(dTile, eoTile, null, null, fd, ref statusCount);
                        formContainer.Items.Add(dTile);
                    }
                    eoTile.Frames[1].Elements[0].Text = String.Format(
                              "Загруженных форм {0}\nС ошибками {1}\nС опозданием {2}\nС ошибками и опозданием {3}\nБез ошибок и опозданий {4}",
                              data.Count, statusCount.FormsWithErrorsCount, statusCount.FormsWithExpiredDateCount, statusCount.FormsWithBothCount, statusCount.FormsOkCount);
                }
                view.ContentContainers.Add(formContainer);
            }
            view.ContentContainers.Add(eduContainer);
            return eduContainer;
        }

        public static TileContainer CreateMunicipalityFormsTileContainer(Municipality m, WindowsUIView view, bool isArchive = false)
        {
            string formCaption = m.Name;
            string formSubCaption = isArchive ? "Архивные формы" : "Формы";

            TileContainer formContainer = TileContainerHelper.CreateTileContainer(view, null, formCaption, formSubCaption);
            formContainer.Tag = TagHelper.GetFormContainerTag(m, AllformType.Type.Types.Municipality, isArchive); //Для добавления плитки с загруженной формой
            TileContainerHelper.InitFormContainerActions(formContainer);
            GuiHelper.FormStatusCount statusCount = new GuiHelper.FormStatusCount() { FormsOkCount = 0, FormsWithErrorsCount = 0, FormsWithExpiredDateCount = 0, FormsWithBothCount = 0 };

            IList<MunicipalityFormData> dataList = RepoHelper.Repo.GetMunitFormData(m, isArchive);
            if (dataList != null && dataList.Count > 0)
            {
                foreach (MunicipalityFormData fd in dataList)
                {
                    Tile dTile = TileHelper.CreateFormTile(fd, view, formContainer, isArchive);
                    dTile.ActivationTarget.Parent = formContainer;
                    TileHelper.SetFormTileStatus(dTile, null, null, null, fd, ref statusCount);
                    formContainer.Items.Add(dTile);
                }
            }
            view.ContentContainers.Add(formContainer);
            return formContainer;
        }
    }
}
